package com.yuan.member.controller;


import cn.common.constant.SmsConstant;
import cn.common.exception.BusinessException;
import cn.common.result.Response;

import cn.model.Member;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.yuan.member.mapper.MemberMapper;
import com.yuan.member.service.MemberService;

import com.yuan.security.annotation.RequiresLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {


    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberService memberService;

    @RequiresLogin
    @GetMapping("/index")
    public Response index() {
       // redisRepository.setExpire("aaa", "testaaa",300);
       // throw new BusinessException("错误了");
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
       List<Member> members = memberMapper.selectList(memberQueryWrapper);
       return Response.Success(members);
    }

    @PostMapping("/register")
    public Response register(@RequestBody Member member,@RequestParam String code) {
        return Response.Success(memberService.register(member,code));
    }

    @PostMapping("/login")
    public Response login(@RequestParam String phone, String password, String areaCode) {
        return Response.Success(memberService.memberLogin(phone, password, areaCode));
    }


    /**
     *
     * 微信登录
     * @param code
     * @return
     */
    @PostMapping("/wechatLogin")
    public Response wechatLogin(@RequestParam("code") String code) {
        return Response.Success(memberService.wechatLogin(code));
    }




    @GetMapping("/info/{phone}")
    public List<Member> getMember(@PathVariable("phone") String phone) {
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("phone",phone);
        //return Response.Success();
//        if(Objects.equals(phone, "11111111111")){
//            throw new BusinessException("获取错误");
//        }
        return memberMapper.selectList(memberQueryWrapper);
    }




    @GetMapping("/product")
    public String product() {

       return memberService.getProduct(1);
    }

    @GetMapping("/buy")
    public String buy() {
        return memberService.getProduct(2);
    }

    @GetMapping("/list")
    public String list() {
        HashMap<String,Object> hashMap =  new HashMap<>();
        hashMap.put("account","127123123");
        hashMap.put("msg","test");
        hashMap.put("type","1");
        memberMapper.insertMessage(hashMap);
        return "list";
    }


    //验证用户验证码
    @PostMapping("/checkSmsCode")
    public Response checkSmsCode(@RequestParam String phone, @RequestParam String code, SmsConstant.Sms_Type type) {
       return Response.Success(memberService.checkSmsCode(phone,code,type));
    }
    //获取个人详情
    @GetMapping("/getMember")
    @RequiresLogin
    public Response getMember() {
        return Response.Success(memberService.getMember());
    }

    //币种查询余额
    @PostMapping("/queryBalanc")
    @RequiresLogin
    public Response queryBalanc(String Symbol) {
        return Response.Success(memberService.queryBalance(Symbol));
    }
    @RequiresLogin
    @GetMapping("/code")
    public Response getCode() {
        return Response.Success(memberService.getCode());
    }

    @RequiresLogin
    @GetMapping("/selectInviteRecord")
    public Response selectInviteRecord() {
        return Response.Success(memberService.selectInviteRecord());
    }
}
